package com.yzz.composeweatherproject.activity.side_effects

import android.graphics.Movie
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.rememberCoroutineScope
import kotlinx.coroutines.launch
import java.io.Serializable

/**
 * @Author : yangzuozhe
 * @Time : On 2022/5/31 20:47
 *
 * @Description : SideEffectsMoudle
 */
// 将类型标记为稳定以支持跳过和智能重组。
@Stable
interface UiState<T> {
    val value: T?
    val exception: Throwable?

    val hasError: Boolean
        get() = exception != null
}

data class UiBean(var name: String = "")

@Composable
fun MyScreen(
    scaffoldState: ScaffoldState = rememberScaffoldState(),
) {
    // 如果 UI 状态包含错误，则显示快餐栏
    LaunchedEffect(key1 = scaffoldState.snackbarHostState) {
    Log.d("yyzzlaunch","init")
//使用协程显示 snackbar ，当协程被取消时 snackbar 会自动关闭。
// 当 `state.hasError` 为 false 时，此协程将取消，
// 并且仅在 `state.hasError` 为 true（由于上述 if-check）或 `scaffoldState.snackbarHostState` 更改时启动。
        scaffoldState.snackbarHostState.showSnackbar(
            message = "Error message",
            actionLabel = "Retry message"
        )

    }
    Scaffold(scaffoldState = scaffoldState) {

    }

}

@Composable
fun MoviesScreen(scaffoldState: ScaffoldState = rememberScaffoldState()) {

    // 创建一个绑定到 MoviesScreen 生命周期的 CoroutineScope
    val scope = rememberCoroutineScope()

    Scaffold(scaffoldState = scaffoldState) {
        Column {
            /* ... */
            Button(
                onClick = {
                    // 在事件处理程序中创建一个新的协程以显示一个快餐栏
                    scope.launch {
                        scaffoldState.snackbarHostState.showSnackbar("Something happened!")
                    }
                }
            ) {
                Text("Press me")
            }
        }
    }
}